package com.cs51.seam;
/* Returns a SeamMap.
 */
public class SeamMap {

    private final double MAX_SINGLE_ENERGY = 1;

    /* The path cost to a given node from the top edge */
    double[][] seamMap;
    /* The EnergyMap used to derive the SeamMap*/
    private EnergyMap energyMap;
    
    private int width;
    private int height;


    /* SeamMap constuctor.
     * Takes an EnergyMap and returns a completed SeamMap 
     */
    public SeamMap(EnergyMap eMap) {
       energyMap = eMap;
       width = energyMap.getWidth();
       height = energyMap.getHeight();
       
       mapSeams();  
    }
    
    
    private void mapSeams() {
        int x=this.width, y=this.height;
        this.seamMap = new double[this.width][this.height];
        
        for(;y>0;y--) {
            for(;x>0;x--) {
               seamMap[x][y] = getPathEnergy(x, y);       
            }
        }
    }
    
private double getPathEnergy(int x, int y) {
        double e = MAX_SINGLE_ENERGY;
        int n = x;
        
        /* For top row, just give the energy of the actual pixel */
        if(y == this.height-1) { return this.energyMap.getEnergy(x, y); }
        
        if (x > 0) { 
            n = x-1;
            e = this.energyMap.getEnergy(x-1, y);
        }
        if(this.energyMap.getEnergy(x, y) < e) {
            n = x;
            e = this.energyMap.getEnergy(x, y);
        }
        if(x < width && (this.energyMap.getEnergy(x+1, y) < e) ) {
            n = x+1;
        }
        return this.energyMap.getEnergy(n, y-1);
    }

}
